package com.xiaobao.campusLostFoundAfter.controller;

import com.xiaobao.campusLostFoundAfter.common.Result;
import com.xiaobao.campusLostFoundAfter.pojo.Views;
import com.xiaobao.campusLostFoundAfter.service.ViewsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/Views")
public class ViewsController {
    private final ViewsService viewsService;

    public ViewsController(ViewsService viewsService) {
        this.viewsService = viewsService;
    }

    //添加浏览次数
    @GetMapping("/add")
    public String addViews(Integer itemId, String type) {
        int result = viewsService.addViews(itemId, type);
        if (result == 1) {
            return "添加成功";
        } else {
            return "添加失败";
        }
    }

    //获取浏览次数
    @GetMapping("/FoundItemGet/{itemId}")
    public Result<Views> getViews(@PathVariable Integer itemId) {
        return viewsService.getViews(itemId);
    }

    /**
     * 获取丢失物品的浏览次数
     * 该方法通过GET请求接收一个itemId参数，然后调用viewsService的getViewsLost方法
     * 获取与该itemId相关的浏览次数信息，并将结果返回给客户端
     *
     * @param itemId 物品的唯一标识符，用于查询特定物品的浏览次数
     * @return 返回一个Result对象，其中包含Views对象，表示浏览次数的信息
     */
    @GetMapping("/LostItemGet/{itemId}")
    public Result<Views> getViewsLost(@PathVariable Integer itemId) {
        // 调用服务方法获取特定itemId的浏览次数信息
        // 返回获取到的浏览次数信息
        return viewsService.getViewsLost(itemId);
    }
}
